import {Component} from 'react'
import {FlowPortWidget} from "../../flow/FlowPortWidget"
import {FlowUnitWrapper} from "../../unitWrapper/FlowUnitWrapper"
import {FlowConst} from "../../../Const";

export class BaseNodeWidget extends Component {
    static defaultProps = {
        size: FlowConst.DEFAULT_COMPONENT_SIZE,
        strokeMiterlimit : 10,
        stroke : FlowConst.DEFAULT_COMPONENT_STROKE,
        strokeWidth: FlowConst.DEFAULT_COMPONENT_STROKE_WIDTH,
    };

    constructor (props){
        super(props)
    }



    getNode (shapeNode, displayOnly){
        let node = shapeNode

        if (displayOnly) {
            node = new BaseNodeModel()
        }
        return node
    }

    getPort (props) {
        const {node, color, displayOnly} = this.props
        let diamondNode = this.getNode(node, displayOnly)

        return <FlowPortWidget node={diamondNode}   {...props}/>
    }

    createShape (props){
        throw new EventException("You must implement it in the sub class to determine the specific shape in points!s")
    }

    getCommonPolygonProps (props){
        let isSelected = false
        if(props.node && typeof props.node.isSelected === 'function'){
            isSelected = props.node.isSelected()
        }

        return {
            stroke : isSelected ? FlowConst.DEFAULT_COMPONENT_SHADOW_STROKE : FlowConst.DEFAULT_COMPONENT_STROKE,
        }
    }


    render () {
        const {size, displayOnly, text, ...attributes} = this.props
        const polygonProps = Object.assign({}, BaseNodeWidget.defaultProps)
        Object.assign(polygonProps, this.props)
        Object.assign(polygonProps, this.getCommonPolygonProps(this.props))

        return (
            <FlowUnitWrapper
                width={polygonProps.size}
                height={polygonProps.size}
                text={polygonProps.name}
                displayOnly={polygonProps.displayOnly}
                {...attributes}
            >
                {this.createShape(polygonProps)}
            </FlowUnitWrapper>
        )
    }
}
